package com.demo.module.controller;

import com.demo.module.model.AdminInteractionStatisticsModel;
import com.demo.module.model.InteractionActiveUserModel;
import com.demo.module.model.InteractionColumnClickModel;
import com.demo.module.model.InteractionStockEvaluationModel;
import com.demo.module.param.UserBehaviorSaveParams;
import com.demo.module.service.ISzyUserBehaviorService;
import com.demo.module.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

/**
 * 首页相关接口
 * @author hanfei
 * @date 2023-11-03
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class SzyAdminController {

    @Autowired
    private ISzyUserBehaviorService behaviorService;


    @GetMapping(value = "/interaction")
    public Result<?> queryInteractionStatistics(
            @RequestParam(name="startTime",required = false) String startTime,
            @RequestParam(name="endTime",required = false) String endTime
    ){
        AdminInteractionStatisticsModel result = new AdminInteractionStatisticsModel();
        //获取栏目点击信息
        log.info("获取栏目点击信息开始:{}",System.currentTimeMillis());
        InteractionColumnClickModel columnClickModel = behaviorService.getColumnClick(startTime,endTime);
        log.info("获取栏目点击信息结束:{}",System.currentTimeMillis());
        //获取股评数据
        log.info("获取股评信息开始:{}",System.currentTimeMillis());
        InteractionStockEvaluationModel stockEvaluationModel = behaviorService.getStockEvaluation(startTime,endTime);
        log.info("获取股评信息结束:{}",System.currentTimeMillis());
        //获取活跃用户排名
        log.info("获取活跃用户排名开始:{}",System.currentTimeMillis());
        List<InteractionActiveUserModel> activeUserModel = behaviorService.getActiveUser(startTime,endTime);
        log.info("获取活跃用户排名结束:{}",System.currentTimeMillis());
        result.setActiveUser(activeUserModel);
        result.setColumnClick(columnClickModel);
        result.setStockEvaluation(stockEvaluationModel);
        return Result.OK(result);
    }

    @PostMapping("/save")
    public Result<?> save(@RequestBody UserBehaviorSaveParams params, HttpServletRequest request){
        return behaviorService.saveBehavior(params);
    }


    private boolean isTimeValid(String time){
        // 检查time是否为空字符串
        if (!Optional.ofNullable(time).isPresent()) {
            return false;
        }
        return true;
    }
}
